package com.goodvideotutorials.spring.mail;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

//@Primary
@Component
public class SmtpMailSender implements MailSender {

	private static final Logger logger = LoggerFactory.getLogger(SmtpMailSender.class);
	
	@Autowired
	private JavaMailSender javaMailSender;
	
	/* (non-Javadoc)
	 * @see com.goodvideotutorials.spring.mail.MailSender#send(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void send(String to, String subject, String body) throws MessagingException{
		
		MimeMessage message = javaMailSender.createMimeMessage();
		MimeMessageHelper helper;
		
		helper = new MimeMessageHelper(message, true); // true indicates
													   // multipart message
		helper.setSubject(subject);
		helper.setTo(to);
		helper.setText(body, false); // true indicates html
		// continue using helper object for more functionalities like adding attachments, etc.  
		
		helper.addAttachment("test.html", new File("/Users/FatemehSalehi/Documents/test.html"));
		
		logger.info("Sending an email...");
		
		javaMailSender.send(message);
		
		logger.info("Email sent.");
		
		
	}

}
